package com.ck.localdatetime;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;

/**
 * JDK8中提供了三个新的时间类,LocalDate(日期处理类)、LocalDateTime(日期时间处理类)、LocalTime(时间处理类)
 *
 * 在使用SimpleDateFormat的时候, 是线程不安全的。
 * Date打印出来的日期不经过格式化, 可读性很差
 */


public class Demo {
    public static void main(String[] args) {
        //test1();
        //test2();
        test3();
    }


    /**
     * LocalDate
     */
    public static void test1(){
        //获取当前时间
        LocalDate now = LocalDate.now();
        System.out.println("当前时间: "+now);

        //设置日期
        LocalDate localDate = LocalDate.of(2020, 3, 17);
        //获取年
        int year = localDate.getYear();
        System.out.println(year);
        //获取月
        int month = localDate.getMonthValue();
        System.out.println(month);
        //获取日
        int day = localDate.getDayOfMonth();
        System.out.println(day);

        //获取星期
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        System.out.println("星期: "+dayOfWeek);
        int dayOfwWek1 = localDate.get(ChronoField.DAY_OF_WEEK);
        System.out.println("一周的第几天: "+dayOfwWek1);
    }

    /**
     * LocalTime
     */
    public static void test2(){
        //获取当前时间
        LocalTime now = LocalTime.now();
        System.out.println("当前时间: "+now);

        //设置时间
        LocalTime localTime = LocalTime.of(17,12,12);
        System.out.println("设置的时间: "+localTime);
        //获取小时
        int hour = localTime.getHour();
        System.out.println("时: "+hour);
        //获取分钟
        int minute = localTime.getMinute();
        System.out.println("分钟: "+minute);
        //获取秒
        int second = localTime.getSecond();
        System.out.println("秒: "+second);
    }

    /**
     * LocalDateTime
     */
    public static void test3(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间: "+now);
        System.out.println("格式化后: "+now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}
